//
//  NetWorkManager.m
//  QuickCure
//
//  Created by 马文星 on 16/4/30.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "NetWorkManager.h"
#import "UserDTO.h"

@implementation NetWorkManager

//单例
+(instancetype)sharedManager{
    
    static NetWorkManager *instance = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        //1.
        instance = [[NetWorkManager alloc] init];
    });
    return instance;
}


- (void)loadWeatherWithCompleteBlock:(void(^)(NSString *nightPictureUrl, NSString *weather,NSString *city,NSString *temperature,NSString *dateString))completeBlock{
    
    
    //2016-04-30 22:38:12.173 QuickCure[69092:2925894] [fg0,0,255;城市：上海[;
    //2016-04-30 22:38:12.174 QuickCure[69092:2925894] [fg0,0,255;nightPictureUrl = http://api.map.baidu.com/images/weather/night/qing.png
    //       晴
    //       28 ~ 17℃
    //       周六 04月30日 (实时：19℃)[;
    NSString *city = @"上海";
    NSString *url = @"http://api.map.baidu.com/telematics/v3/weather";
    
    NSDictionary *paramDic = @{@"location":city,
                               @"output":@"json",
                               @"ak":@"17IvzuqmKrK1cGwNL6VQebF9"
                               };
    
    [[NetWorkTool shareManager] GET_RequestWithUrlString:url parmas:paramDic successBloock:^(id responseObject) {
        
        //        NSLog(@"天气= %@",responseObject[@"results"][0]);
        
        NSDictionary *responseDic= responseObject[@"results"][0];
        
        
        WeatherModel *model =  [WeatherModel yy_modelWithDictionary:responseDic];
        
        //        NSLog(@"model = %@",model);
        
        NSString *city = model.currentCity;
        //        loggerImpt(@"城市：%@",city);
        NSArray *everyDayModel = model.weather_data;
        EveryDayWeatherModel *everyModel = everyDayModel[0];
        
        NSString *nightPictureUrl = everyModel.nightPictureUrl;
        NSString *weather = everyModel.weather;
        NSString *temperature = everyModel.temperature;
        
        //拼接温度
        NSString *temperatureString = [NSString stringWithFormat:@"温度：%@",temperature];
        
        NSString *date = everyModel.date;
        //        loggerImpt(@"nightPictureUrl = %@\n%@\n%@\n%@",nightPictureUrl,weather,temperature,temperatureString);//周六 04月30日 (实时：19℃)
        NSString *dateS = [date substringWithRange:NSMakeRange(3, 6)];
        //拼接日期
        NSString *dateString = [NSString stringWithFormat:@"日期：%@",dateS];
        NSLog(@"dateString = %@",dateString);
        
        completeBlock(nightPictureUrl,weather,city,temperatureString,dateString);
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"天气错误= %@",error);
    }];
}


-(void)loginWithLoadUserInfo:(void(^)(UserDTO *dto))completeBlock{

    
    NSString *urlString = kLoginUrl;
    NSDictionary *paramDic = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f"};

    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        
        loggerInfo(@"responseObject = %@",responseObject[@"data"]);
        
        NSDictionary *dic = responseObject[@"data"];
        
        
        loggerInfo(@"dic = %@",dic);
        
         UserDTO *dto = [UserDTO yy_modelWithDictionary:dic];
        
        completeBlock(dto);
        
    } failureBlock:^(NSError *error) {
        
        loggerError(@"error= %@",error);
    }];


}

@end
